package engine.graphics;

import java.io.Serializable;
import java.util.ArrayList;

import engine.system.Timer;

public class SpriteAnimation implements IRender, Serializable {

	private static final long serialVersionUID = 1L;
	
	private SpriteSheet sprites ;
	private ArrayList<String> spriteNames ; 
	private int spriteID = 0 ; 
	private Sprite currentSprite = null ; 
	private int delta = 0; 
	private Timer timer ;  
	
	public SpriteAnimation(){
		sprites = new SpriteSheet(); 
		spriteNames = new ArrayList<String>(); 
		this.setDelta(delta);
		this.timer = new Timer() ;
		this.timer.start(); 
	}
	
	public SpriteAnimation(SpriteSheet sprites, int delta){
		this();
		this.sprites = sprites ; 
		for(String s: sprites.getKeySet())
			spriteNames.add(s);
		this.currentSprite = sprites.getSpriteByName(getNext()); 
	}

	public SpriteSheet getSprites() {
		return sprites;
	}
	
	public int getDelta() {
		return delta;
	}

	public void setDelta(int delta) {
		this.delta = delta;
	}

	private String getNext(){
			if(spriteID>=spriteNames.size()-1)
				spriteID = 0 ; 
			else
				spriteID++ ; 
			return spriteNames.get(spriteID);  
	}
	
	public void addNewSprite(String name, Sprite sprite){
		this.spriteNames.add(name);
		this.sprites.addNewSprite(name, sprite); 
	}
	
	@Override
	public void update() {
		if(this.sprites.getSize()>1){
			if(this.timer.getAbsoluteDelta()>delta){
				this.currentSprite = this.sprites.getSpriteByName(this.getNext());
				this.timer.start(); 
			}
		}else 
			this.currentSprite = this.sprites.getSpriteByName(this.spriteNames.get(0));
	}
	
	public Sprite getCurrentSprite(){
		return this.currentSprite; 
	}

	@Override
	public void render() { }
	
	
	
	
}
